<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/28
 * Time: 19:00
 */
namespace SwoStar\Server\Http;

use SwoStar\Message\Http\Request as HttpRequest;

use SwoStar\Console\Input;
use SwoStar\Server\Server ;
use Swoole\Http\Server as SwooleServer;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;

class HttpServer extends Server
{

    // 初始化默认设置
    protected function initSetting()
    {
        $config = app('config');
        $this->port = $config->get('server.http.port');
        $this->host = $config->get('server.http.host');
        $this->config = $config->get('server.http.swoole');
    }
    /**
     * 创建服务
     * 六星教育 @shineyork老师
     * http server
     */
    public  function createServer()
    {
        $this->swooleServer=new SwooleServer($this->host,$this->port);
        Input::info('http server 访问 http://192.168.157.240:9000');
    }
    /**
     * 初始化监听的事件
     * 六星教育 @shineyork老师
     */
    protected  function initEvent()
    {
        $this->setEvent('sub',[
            'request'=>'onRequest',
        ]);
    }

    public  function onRequest(SwooleRequest $request,SwooleResponse $response)
    {

        $uri = $request->server['request_uri'];

        if ($uri == '/favicon.ico') {
            $response->status(404);
            $response->end('');
            return null;
        }
        $httpRequest = HttpRequest::init($request);
//        dd($httpRequest->getMethod(), "Method");
//        dd($httpRequest->getUriPath(), "UriPath");//  /index

        //执行控制器的方法
        $return=app('route')->setFlag('Http')->setMethod($httpRequest->getMethod())->match($httpRequest->getUriPath());
        $response->end($return);
    }
}